uniform extern float4x4 mW;
uniform extern float4x4 mWIT;
uniform extern float4x4 mWVP;
uniform extern float3 f3VPos;

uniform extern float4 f4AI;
uniform extern float4 f4DI;
uniform extern float4 f4SI;

uniform extern float3 f3LDir;

uniform extern float3 f3LPos;
uniform extern float fA0;
uniform extern float fA1;
uniform extern float fA2;

uniform extern float4 f4AM;
uniform extern float4 f4DM;
uniform extern float4 f4SM;
uniform extern float fSP;

struct Vertex
{
	float4 f4PosH	: POSITION0;
	float3 f3NormW	: TEXCOORD0;
	float3 f3PosW	: TEXCOORD1;
};

Vertex Trans(float3 f3PosL: POSITION0, float3 f3NormL: NORMAL0)
{
	Vertex vout = (Vertex)0;
	
	vout.f4PosH = mul(float4(f3PosL,1.0f),mWVP);
	vout.f3NormW = normalize(mul(float4(f3NormL,0.0f),mWIT).xyz);
	vout.f3PosW = mul(float4(f3PosL,1.0f),mW).xyz;

	return vout;
}

float4 Phong(float3 f3NormW, float3 f3PosW, float fAtt, float3 f3LVec)
{
	float3 f3A = fAtt*(f4AI*f4AM).rgb;
	float3 f3D = fAtt*max(dot(f3LVec,f3NormW),0.0f)*(f4DI*f4DM).rgb;
	float3 f3S = fAtt*pow(max(dot(reflect(-f3LVec,f3NormW),normalize(f3VPos-f3PosW)),0.0f),fSP)*(f4SI*f4SM).rgb;

	return float4(f3A+f3D+f3S,1.0);
}

float4 Point(float3 f3NormW: TEXCOORD0, float3 f3PosW: TEXCOORD1) : COLOR
{
	float3 f3O2L = f3LPos-f3PosW;
	float fDist = abs(f3O2L);
	return Phong( normalize(f3NormW), f3PosW, 1.0f/(fA0+fA1*fDist+fA2*fDist*fDist), normalize(f3O2L) );
}

float4 Direct(float3 f3NormW: TEXCOORD0, float3 f3PosW: TEXCOORD1) : COLOR
{
	return Phong( normalize(f3NormW), f3PosW, 1.0f, f3LDir );
}

technique MultiLightPass
{
	pass _0
	{
		vertexShader = compile vs_2_0 Trans();
		pixelShader  = compile ps_2_0 Direct();
	}
	pass _1
	{
		vertexShader = compile vs_2_0 Trans();
		pixelShader  = compile ps_2_0 Point();
	}
}
